package com.lianniu.mgr.baseService.post.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.baseService.area.service.ISystemAreaService;
import com.lianniu.mgr.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.post.service.IAssignAuthorityService;
import com.lianniu.mgr.baseService.post.service.ICheckAuthorityService;

@Controller
@RequestMapping("/rest")
public class AssignAuthorityRestController {
  @Resource
  private IAssignAuthorityService assignAuthorityService;

  @Resource
  private ICheckAuthorityService checkAuthorityService;

  @Resource
  private ISystemAreaService systemAreaService;

  private final static Logger logger=Logger.getLogger(AssignAuthorityRestController.class);

  /**
   * @Description:分配菜单
   * @return BaseResult(data=List<MenuVo>)

   * @Date:2016年3月29日下午8:48:04
   * @Version 1.0.0
   */
  @RequestMapping(value="/assignMenu")
  public @ResponseBody BaseResult assignMenuList(HttpServletRequest request){
    logger.info("AssignAuthorityRestController assignMenuList begin.");
    logger.info("postId:"+request.getParameter("postId"));
    String postId=request.getParameter("postId");
    BaseResult baseResult1=null;
    BaseResult baseResult2=null;
    try{
      logger.info("getAllMenu begin.");
      baseResult1=assignAuthorityService.getAllMenu();
      logger.info("getAllMenu end.");
      logger.info("AssignAuthorityRestController assignMenuList end.");
    }catch(Exception e){
      logger.error("getAllMenu error:"+e.getMessage());
      logger.info("AssignAuthorityRestController assignMenuList end.");
      return BaseResult.exception(e.getMessage());
    }

    try{
        logger.info("getCheckedMenuByPostId begin.");
        baseResult2=assignAuthorityService.getCheckedMenuByPostId(postId);
        logger.info("getCheckedMenuByPostId end.");
        logger.info("AssignAuthorityRestController getCheckedMenuByPostId end.");
      }catch(Exception e){
        logger.error("getCheckedMenuByPostId error:"+e.getMessage());
        logger.info("AssignAuthorityRestController assignMenuList end.");
        return BaseResult.exception(e.getMessage());
      }

    Map<String,Object> retMap=new HashMap<String,Object>();
    retMap.put("allMenu", baseResult1.getData());
    retMap.put("selectedMenu", baseResult2.getData());

    logger.info("AssignAuthorityRestController assignMenuList end.");
    baseResult1.setData(retMap);
    return baseResult1;

  }

  /**
   * @Description:保存分配的菜单
   * @param request
   * @return BaseResult

   * @Date:2016年4月1日下午1:19:37
   * @Version 1.0.0
   */
  @RequestMapping(value="/saveMenu",method=RequestMethod.POST)
  public @ResponseBody BaseResult saveMenu(HttpServletRequest request){
    logger.info("AssignAuthorityRestController saveMenu begin.");
    logger.info("menuIdArr:"+request.getParameterValues("menuIdArr"));
    logger.info("postId:"+request.getParameter("postId"));

    String[] menuIdArr=request.getParameterValues("menuIdArr");
    String postId=request.getParameter("postId");
    try{
      logger.info("addMenu begin.");
      BaseResult baseResult=assignAuthorityService.addMenu(menuIdArr, postId);
      logger.info("addMenu end.");
      logger.info("AssignAuthorityRestController saveMenu end.");
      return baseResult;
    }catch(Exception e){
      logger.error("addMenu error:"+e.getMessage());
      logger.info("AssignAuthorityRestController saveMenu end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:分配辖区
   * @return BaseResult(data=List<Staff2SysAreaVo>)
   * @Date:2016年3月29日下午8:48:50
   * @Version 1.0.0
   */
  @RequestMapping(value = "/assignPrecinct", method = RequestMethod.GET)
  public @ResponseBody BaseResult assignPrecinctList(HttpServletRequest request, String staffId) {
    logger.info("StaffRestController assignPrecinctList begin.");
    logger.info("staffId:" + request.getParameter("staffId"));

    logger.info("StaffRestController assignPrecinctList input data check begin.");
    if (StringUtil.isEmpty(staffId)) {
      logger.error("入参staffId为空");
      logger.info("StaffRestController assignPrecinctList input data check end");
    }

    try {
      logger.info("StaffRestController assignPrecinctList getProToCouAreaByStaffId begin.");
      BaseResult baseResult = systemAreaService.getProToCouAreaByStaffId(staffId);
      logger.info("StaffRestController assignPrecinctList getProToCouAreaByStaffId end.");
      logger.info("StaffRestController assignPrecinctList end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController assignPrecinctList getProToCouAreaByStaffId error:", e);
      logger.info("StaffRestController assignPrecinctList end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:保存分配的区域
   * @param
   * @return BaseResult
   * @Date:2016年4月1日下午1:19:37
   * @Version 1.0.0
   */
  @RequestMapping(value = "/savePrecinct", method = RequestMethod.POST)
  public @ResponseBody BaseResult savePrecinct(String staffId, String userName, String[] areaIdArr,
                                               String[] oldAreas, String[] newAreas, String[] rootNodes) {
    logger.info("StaffRestController savePrecinct begin.");
    logger.info("区域id数组&areaIdArr:" + JSON.toJSONString(areaIdArr));
    logger.info("登陆者&userName:" + userName);
    logger.info("oldAreas名:" + JSON.toJSONString(oldAreas));
    logger.info("newAreas名:" + JSON.toJSONString(newAreas));
    logger.info("rootNodes区域树根节点id:" + JSON.toJSONString(rootNodes));
    logger.info("员工id&staffId:" + staffId);

    try {
      logger.info("StaffRestController savePrecinct addPrecinct begin.");
      BaseResult baseResult = systemAreaService.addPrecinct(staffId, userName, areaIdArr, oldAreas,
              newAreas, rootNodes);
      logger.info("StaffRestController savePrecinct addPrecinct end.");
      logger.info("AssignAuthorityRestController savePrecinct end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController savePrecinct addPrecinct error:", e);
      logger.info("StaffRestController savePrecinct end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:保存分配的功能
   * @param request
   * @return BaseResult

   * @Date:2016年4月5日下午3:05:42
   * @Version 1.0.0
   */
  @RequestMapping(value="/saveOperation",method=RequestMethod.POST)
  public @ResponseBody BaseResult saveOperation(HttpServletRequest request){
	logger.info("AssignAuthorityRestController saveOperation begin.");
	logger.info("opArr:"+request.getParameterValues("opArr"));
	logger.info("postId:"+request.getParameter("postId"));
	String[] opArr=request.getParameterValues("opArr");
    String postId=request.getParameter("postId");
	try{
		logger.info("addOperation begin.");
		BaseResult baseResult=assignAuthorityService.addOperation(opArr, postId);
		logger.info("addOperation end.");
		logger.info("AssignAuthorityRestController saveOperation end.");
		return baseResult;
	}catch(Exception e){
		logger.error("addOperation error:"+e.getMessage());
		logger.info("AssignAuthorityRestController saveOperation end.");
		return BaseResult.exception(e.getMessage());
	}
  }

  /**
   * @Description:分配功能操作
   * @param request
   * @return BaseResult
   * @Date:2016年4月8日上午11:10:11
   * @Version 1.0.0
   */
  @RequestMapping(value="/assignOperation",method=RequestMethod.GET)
  public @ResponseBody BaseResult assignOperation(HttpServletRequest request){
    logger.info("AssignAuthorityRestController assignOperation begin.");
    logger.info("postId:"+request.getParameter("postId"));

    String postId=request.getParameter("postId");

    BaseResult baseResult1=null;
    BaseResult baseResult2=null;
    try{
      logger.info("getSysMenuByPostId begin.");
      baseResult1=assignAuthorityService.getSysMenuByPostId(postId);
      logger.info("getSysMenuByPostId end.");
    }catch(Exception e){
      logger.error("getSysMenuByPostId error:"+e.getMessage());
      logger.info("AssignAuthorityRestController assignOperation end.");
      return BaseResult.exception(e.getMessage());
    }

    try{
      logger.info("getCheckedOpByPostId begin.");
      baseResult2=assignAuthorityService.getCheckedOpByPostId(postId);
      logger.info("getCheckedOpByPostId end.");
    }catch(Exception e){
      logger.error("getCheckedOpByPostId error:"+e.getMessage());
      logger.info("AssignAuthorityRestController assignOperation end.");
      return BaseResult.exception(e.getMessage());
    }

    Map<String,Object> retMap=new HashMap<String,Object>();
    retMap.put("sysMenu", baseResult1.getData());
    retMap.put("checkedOp", baseResult2.getData());

    logger.info("AssignAuthorityRestController assignOperation end.");
    baseResult1.setData(retMap);
    return baseResult1;
  }

  /**
   * @Description:根据menuId查询功能名称
   * @param request
   * @return BaseResult

   * @Date:2016年4月10日下午6:26:04
   * @Version 1.0.0
   */
  @RequestMapping(value="/getOpByMenuId",method=RequestMethod.GET)
  public @ResponseBody BaseResult getOpByMenuId(HttpServletRequest request){
    logger.info("AssignAuthorityRestController getOpByMenuId begin.");
    logger.info("menuId:"+request.getParameter("menuId"));
    String menuId=request.getParameter("menuId");
    try{
      logger.info("getOpByMenuId begin.");
      BaseResult baseResult=assignAuthorityService.getOpByMenuId(menuId);
      logger.info("getOpByMenuId end.");
      logger.info("AssignAuthorityRestController getOpByMenuId end.");
      return baseResult;
    }catch(Exception e){
      logger.error("getOpByMenuId error:"+e.getMessage());
      logger.info("AssignAuthorityRestController getOpByMenuId end.");
      return BaseResult.exception(e.getMessage());
    }
  }
}
